package com.karrim.server.portable;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.karrim.server.annotation.Annotation;
import com.karrim.server.annotation.Annotation.Portable.PortableSet;
import com.karrim.server.annotation.Annotation.ScannedPackage;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.reflections.Reflections;

/**
 *
 * @author andrea.cantafio
 */
@Singleton
public class PortableClassScanner extends Thread {

    @Inject
    public PortableClassScanner(@ScannedPackage String basePackage, @PortableSet Set<Class<?>> portables) {
        super("PortableClassDiscoverer");
        start();
    }

    @Override
    public void run() {
        Reflections reflections = new Reflections(basePackage);
        Set<Class<?>> portables = reflections.getTypesAnnotatedWith(Annotation.Portable.class);
        HashMap<Class<?>, Field> pulls = new HashMap<>();
        HashMap<Class<?>, Field> ignores = new HashMap<>();
        for (Class<?> portable : portables) {
            Logger.getLogger(PortableClassScanner.class.getName()).log(Level.INFO, "Parsing portable class {0}", portable.getCanonicalName());
            for (Field field : portable.getFields()) {
                if (field.isAnnotationPresent(Annotation.Portable.Pull.class)) {
                    pulls.put(portable, field);
                } else {
                    ignores.put(portable, field);
                }
            }
        }
    }
    private String basePackage;
}